﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using DataAccess;
using Table = CrystalDecisions.CrystalReports.Engine.Table;

public partial class ReportePlanAccion : System.Web.UI.Page
{
    #region Propiedades Publicas
    //paso de un parametro
    public ParameterField crtParamField = new ParameterField();
    public ParameterFields crtParamFields = new ParameterFields();
    public ParameterDiscreteValue paramValue = new ParameterDiscreteValue();



    public ParameterField crtParamField2 = new ParameterField();
    public ParameterFields crtParamFields2 = new ParameterFields();
    public ParameterDiscreteValue paramValue2 = new ParameterDiscreteValue();


    public ReportDocument rep = new ReportDocument();

    private DataTable AdmRubros
    {

        get
        {

            DataTable objTable = null;
            if (Cache["AdmRubros" + Session.SessionID] != null)
                objTable = (DataTable)Cache["AdmRubros" + Session.SessionID];
            return objTable;
        }

        set
        {
            if (value != null)
            {
                Cache.Insert("AdmRubros" + Session.SessionID, value, null, DateTime.Now.AddMinutes(20), TimeSpan.Zero);
            }
        }
    }

    #endregion
        
    protected void Page_Load(object sender, EventArgs e)
    {
     

        try
        {
            ConnectionInfo connInfo = new ConnectionInfo();
            connInfo.ServerName = ConfigurationManager.AppSettings["ServerName"];
            connInfo.DatabaseName = ConfigurationManager.AppSettings["DatabaseName"];
            connInfo.UserID = ConfigurationManager.AppSettings["UserID"];
            connInfo.Password = ConfigurationManager.AppSettings["Password"];
            rep.Load(Server.MapPath("Reportes\\TD plan accion rubro.rpt"), OpenReportMethod.OpenReportByTempCopy);
            CrystalReportViewer1.ReportSource = rep;
            Tables repTbls = rep.Database.Tables;

            paramValue.Value = Session["PLN_ID"].ToString();
            crtParamField.ParameterFieldName = "@PLN_ID";
            crtParamField.CurrentValues.Add(paramValue);
            crtParamFields.Add(crtParamField);

            //paramValue2.Value = "2012";
            //crtParamField2.ParameterFieldName = "@PLN_ANO";
            //crtParamField2.CurrentValues.Add(paramValue2);//crtParamFields.Add(crtParamField2);



            foreach (Table repTbl in repTbls)
            {
                TableLogOnInfo repTblLogonInfo = repTbl.LogOnInfo;
                repTblLogonInfo.ConnectionInfo = connInfo;
                repTbl.ApplyLogOnInfo(repTblLogonInfo);
                repTbl.Location = ConfigurationManager.AppSettings["DatabaseName"].ToString() + ".dbo." + repTbl.Name;
            }
            //Session["repPuntajeEstudiantesXPrograma"] = rep;
            //CrystalReportViewer1.ReportSource = rep;
            //CrystalReportViewer1.DataBind();
            //CrystalReportViewer1.ParameterFieldInfo = crtParamFields;
            rep.SetParameterValue("@PLN_ID", Session["PLN_ID"].ToString());
            Response.Buffer = false;
            // Clear the response content and headers
            Response.ClearContent();
            Response.ClearHeaders();
            try
            {
                // Export the Report to Response stream in PDF format and file name Customers
                rep.ExportToHttpResponse(ExportFormatType.PortableDocFormat, Response, true, "Plan de Accion Regionalizado");
                // There are other format options available such as Word, Excel, CVS, and HTML in the ExportFormatType Enum given by crystal reports
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                ex = null;
            }
            rep.Dispose();
            rep.Close();
        }
        catch (Exception ex)
        {
            lblMensaje.Text = ex.Message;
        }

    }
}
